package com.yanqu.road.server.cmd.banquet.crossBanquet;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.banquet.BanquetInfo;
import com.yanqu.road.entity.banquet.UserBanquetInvitation;
import com.yanqu.road.entity.banquet.invitation.BanquetCrossInvitation;
import com.yanqu.road.entity.enums.eBanquetInfoType;
import com.yanqu.road.entity.enums.eBanquetType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.banquet.BanquetProto;
import com.yanqu.road.pb.banquet.CrossBanquetProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.banquet.BanquetModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.banquet.BanquetConfigMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = Protocol.S_BANQUET_CROSS_OPEN, desc = "开宴会")
public class OpenBanquetCmd extends AbstractCommand {

	private int type;//对应的宴会信息的id
	private String invitationId;
	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		BanquetProto.OpenBanquetReq reqMsg = BanquetProto.OpenBanquetReq.parseFrom(packet.getBytes());
		type = reqMsg.getType();
		invitationId = reqMsg.getInvitationId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		//跨服私人宴会
		if(BanquetConfigMgr.isCrossUserBanquet(type)){
			ActivityInfo crbAcInfo = NormalActivityMgr.getRunningCrossBanquetRankActivity();
			//必须是跨服争霸，就是三个区服的才能开宴会
			if(crbAcInfo == null || crbAcInfo.getServerIdList().size() <= 2){
				ret = GameErrorCode.E_BANQUET_CROSS_RANK_ACTIVITY_OVER;
				return;
			}

			BanquetModule banquetModule = player.getModule(BanquetModule.class);
			//开榜重置礼物接受设置,同个活动开始时间内不重置
			if (banquetModule.settingCanReset(type, crbAcInfo.getBeginTime())) {
				banquetModule.updateUserBanquetSetting(type,"");
			}
			banquetModule.updateUserBanquetSettingBanquetId(type, "");

			CrossBanquetProto.PreOpenCrossBanquetReq.Builder msg = CrossBanquetProto.PreOpenCrossBanquetReq.newBuilder();
			msg.setInvitationId("");
			msg.setInvitationInfo("");
			msg.setType(type);
			msg.setServerListStr(crbAcInfo.getServerListStr2());
			msg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.parseUserBaseInfo(player.getUserInfo(), GameServer.getInstance().getServerId())));
			msg.setGiftTypeAccept(banquetModule.getUserBanquetSettingGiftTypeAccept(type));
			YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_PRE_OPEN_BANQUET, msg);
			GamePlayerMgr.sendPacket(player.getUserId(), pbMsg);
			return;
		}
		//跨服盛宴
		if(BanquetConfigMgr.isCrossBanquet(type) || BanquetConfigMgr.isCrossMogulBirthdayBanquet(type)){
			if(!SystemOpenMgr.systemOpen(player, eSystemId.BanquetCross.getValue())){
				ret = GameErrorCode.E_SYSTEM_NO_OPEN;
				return;
			}
			BanquetModule banquetModule = player.getModule(BanquetModule.class);
			if(banquetModule.checkUserInvitation(invitationId,type)){
				if(BanquetConfigMgr.isCrossMogulBirthdayBanquet(type)){
					BanquetInfo banquetInfo = BanquetConfigMgr.getBanquetInfo(eBanquetInfoType.CrossMogulBirthdayBanquet.getValue());
					if(!player.getModule(CurrencyModule.class).removeCurrency(PropertyHelper.parseStringToProperty(banquetInfo.getOpenConsume()), eLogMoneyType.Banquet, eLogMoneyType.BanquetOpenBirthdayConsume)){
						ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
						return;
					}
				}
				UserBanquetInvitation inv = banquetModule.getUserInvitationInfo(invitationId);
				BanquetCrossInvitation invInfo = (BanquetCrossInvitation)inv.getInvitation();
				CrossBanquetProto.PreOpenCrossBanquetReq.Builder msg = CrossBanquetProto.PreOpenCrossBanquetReq.newBuilder();
				msg.setInvitationId(invitationId);
				msg.setInvitationInfo(inv.getInfo());
				msg.setType(type);
				msg.setServerListStr(invInfo.getServerIdsStr());
				msg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.parseUserBaseInfo(player.getUserInfo(), GameServer.getInstance().getServerId())));
				YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_PRE_OPEN_BANQUET, msg);
				GamePlayerMgr.sendPacket(player.getUserId(), pbMsg);
			}else{
				ret = GameErrorCode.E_BANQUET_INVITATION;
			}
			return;
		}
		ret = GameErrorCode.E_BANQUET_INFO_ERROR;
	}

	@Override
	public void sendData(GamePlayer player) {
		if(ret != 0){
			BanquetProto.OpenBanquetResp.Builder respMsg = BanquetProto.OpenBanquetResp.newBuilder();
			respMsg.setRet(ret);
			player.sendPacket(Protocol.U_BANQUET_CROSS_OPEN, respMsg);
		}
	}
}
